package com.db.nearcache.app;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Date;
import java.util.Scanner;

import com.db.nearcache.remote.ObservableRemoteCache;
import com.db.nearcache.remote.RemoteCache;

public class Server {
	private static final String SERVICE_NAME = "foobar";
	private static final int PORT = 4444;

	// no exception handling for simplicity
	public static void main(String[] args) throws Exception {
		RemoteCache cache = new RemoteCache();

		Registry rmiRegistry = LocateRegistry.createRegistry(PORT);
		ObservableRemoteCache cacheService = (ObservableRemoteCache) UnicastRemoteObject
				.exportObject(cache, PORT);
		rmiRegistry.bind(SERVICE_NAME, cacheService);

		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter a key to add (current date will be used as a value):");

		for (;;) {
			System.out.print("> ");
			String input = scanner.nextLine();
			if (input.equals("exit"))
				break;
			cache.put(input, new Date());
		}

		scanner.close();
		rmiRegistry.unbind(SERVICE_NAME);
		System.exit(0);
	}
}
